package qimao.com.edu.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import qimao.com.edu.DTO.BookDTO;
import qimao.com.edu.DTO.ChapterDTO;
import qimao.com.edu.entity.CommenResult;
import qimao.com.edu.entity.Page;
import qimao.com.edu.service.ChaptersService;
import qimao.com.edu.service.WorksService;
import qimao.com.edu.service.impl.ChaptersServiceImpl;
import qimao.com.edu.service.impl.WorksServiceImpl;

@WebServlet("/bookContent.do")
public class BookContentServlet extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		
		ChaptersService cs = new ChaptersServiceImpl();
		int bookId = 1;
		int chapterNum = 1;
		
//		String bookName = request.getParameter("bookName");
//		System.out.println(bookName);
//		String bookWriter = request.getParameter("bookWriter");
		
		try {
	    	if(Integer.parseInt(request.getParameter("bookId"))>0) {
	    		bookId = Integer.parseInt(request.getParameter("bookId"));
	    	}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		try {
	    	if(Integer.parseInt(request.getParameter("chapterNum"))>0) {
	    		chapterNum = Integer.parseInt(request.getParameter("chapterNum"));
	    	}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		ChapterDTO chapterDTO = cs.getChapterDetail(bookId, chapterNum);
//		chapterDTO.setBookName(bookName);
//		chapterDTO.setBookWriter(bookWriter);
		List<ChapterDTO> chapterList = new ArrayList<ChapterDTO>();
		chapterList.add(chapterDTO);
		
		System.out.println(chapterDTO.getUpdateTime());
		
		Page<ChapterDTO> page = new Page<ChapterDTO>();
		page.setData(chapterList);
		
	    CommenResult cr = new CommenResult();
		cr.setCode(200);
		cr.setMsg("success");
		cr.setData(page);
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().write(cr.toJsonString());
	}
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
}
